package actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.StuCourse;
import db.dao.StuCourseDAO;
import db.dao.Student;
import db.dao.StudentDAO;
import db.dao.TeaCourse;
import db.dao.TeaCourseDAO;
import db.dao.Users;
import db.dao.UsersDAO;
/*查找学生选课信息*/
public class SearchStuCourseAction extends ActionSupport{
	private String stucourseOption="";	//查找选项
	private String property="";	//值

	public SearchStuCourseAction() {
	}

	public String getStucourseOption() {
		return stucourseOption;
	}

	public void setStucourseOption(String stucourseOption) {
		this.stucourseOption = stucourseOption;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String execute() throws IOException {
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		//用于传递参数，判断查询内容
		HttpSession session = request.getSession();
		//根据用户选择的查询条件和输入的值查找学生选课信息
		StuCourseDAO scDao = new StuCourseDAO();
		List<StuCourse> list = new ArrayList<StuCourse>();
		Set<StuCourse> sCourses= new HashSet<StuCourse>();
		Iterator<StuCourse> iterator = sCourses.iterator();
		if (stucourseOption.equals("courseid")) {
			list = scDao.findByProperty("id.courseid", property);
		}else if (stucourseOption.equals("studentid")) {
			list = scDao.findByProperty("id.stuid", property);
		}else if (stucourseOption.equals("studentname")) {
			StudentDAO sDao = new StudentDAO();
			List<Student> sList = sDao.findByPropertyLike("stuname", property+"%");
			Iterator<Student> is = sList.iterator();
			while (is.hasNext()) {
				sCourses = is.next().getStuCourses();
				iterator = sCourses.iterator();
				while (iterator.hasNext()) {
					list.add(iterator.next());
				}
			}
		}else if (stucourseOption.equals("coursename")) {
			TeaCourseDAO tcDao = new TeaCourseDAO();
			List<TeaCourse> tcList = tcDao.findByPropertyLike("coursename", property+"%");
			Iterator<TeaCourse> itc = tcList.iterator();
			while (itc.hasNext()) {
				sCourses = itc.next().getStuCourses();
				iterator = sCourses.iterator();
				while (iterator.hasNext()) {
					list.add(iterator.next());
				}
			}
		}
		if (!list.isEmpty()) {
			//成功传递信息
			session.setAttribute("stucourses", list);
			session.setAttribute("search", new String("stucourses"));
			response.setHeader("Pragma","No-cache");   
			response.setHeader("Cache-Control","no-cache");  
			response.setHeader("Cache-Control", "no-store");  
			response.setDateHeader("Expires", 0);
			return "success";
		}else {
			//失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('未查询到相关信息!')</script>");
            UsersDAO uDao = new UsersDAO();
            Users users = uDao.findById((String)session.getAttribute("username"));
            if (users.getUsertype().equals("管理")) {
				printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
			}else if (users.getUsertype().equals("教师")) {
				printWriter.print("<script>window.location.href='/GroupProject/teacher.jsp'</script>");
			}else {
				printWriter.print("<script>window.location.href='/GroupProject/student.jsp'</script>");
			}
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
